#!/bin/bash

# 完整的服务器修复脚本
echo "======================================"
echo "    自动查找并修复后端服务"
echo "======================================"

# 第一步：查找后端文件
echo "1. 查找后端文件位置..."
echo "查找start-server.js:"
START_SERVER_FILES=$(find / -name "start-server.js" 2>/dev/null)
echo "$START_SERVER_FILES"

echo "查找package.json (backend相关):"
PACKAGE_FILES=$(find / -name "package.json" -path "*/backend/*" 2>/dev/null)
echo "$PACKAGE_FILES"

echo "查找app.js (backend相关):"
APP_FILES=$(find / -name "app.js" -path "*/backend/*" 2>/dev/null)
echo "$APP_FILES"

# 确定后端目录
BACKEND_DIR=""
if [ -n "$START_SERVER_FILES" ]; then
    BACKEND_DIR=$(echo "$START_SERVER_FILES" | head -1 | xargs dirname)
    echo "✓ 找到后端目录: $BACKEND_DIR"
elif [ -n "$PACKAGE_FILES" ]; then
    BACKEND_DIR=$(echo "$PACKAGE_FILES" | head -1 | xargs dirname)
    echo "✓ 找到后端目录: $BACKEND_DIR"
elif [ -n "$APP_FILES" ]; then
    BACKEND_DIR=$(echo "$APP_FILES" | head -1 | xargs dirname)
    echo "✓ 找到后端目录: $BACKEND_DIR"
else
    echo "❌ 未找到后端目录，检查可能的位置..."
    ls -la /root/
    ls -la /opt/ 2>/dev/null || echo "无/opt目录"
    ls -la /var/www/ 2>/dev/null || echo "无/var/www目录"
    echo "请手动上传后端代码到服务器"
    exit 1
fi

echo "\n2. 进入后端目录并检查文件..."
cd "$BACKEND_DIR"
echo "当前目录: $(pwd)"
echo "目录内容:"
ls -la

# 检查必要文件
if [ ! -f "package.json" ]; then
    echo "❌ 缺少package.json文件"
    exit 1
fi

if [ ! -f "start-server.js" ] && [ ! -f "app.js" ]; then
    echo "❌ 缺少启动文件"
    exit 1
fi

# 确定启动文件
START_FILE=""
if [ -f "start-server.js" ]; then
    START_FILE="start-server.js"
elif [ -f "app.js" ]; then
    START_FILE="app.js"
fi

echo "使用启动文件: $START_FILE"

echo "\n3. 配置环境变量..."
cat > .env.production << 'EOF'
PORT=8080
NODE_ENV=production
DB_HOST=localhost
DB_USER=cumbull_user
DB_PASSWORD=CuMrBull2024!
DB_NAME=cumbull_db
JWT_SECRET=your-super-secret-jwt-key-2024
CORS_ORIGIN=*
EOF

echo "✓ 环境变量已配置"

echo "\n4. 检查Node.js环境..."
echo "Node.js版本: $(node --version)"
echo "npm版本: $(npm --version)"

echo "\n5. 安装依赖..."
if [ ! -d "node_modules" ]; then
    echo "安装npm依赖..."
    npm install --production
else
    echo "依赖已存在"
fi

echo "\n6. 停止现有Node.js进程..."
pkill -f node || echo "没有运行的Node.js进程"

echo "\n7. 启动服务..."
mkdir -p /var/log
nohup node "$START_FILE" > /var/log/backend.log 2>&1 &
SERVICE_PID=$!
echo "服务已启动，PID: $SERVICE_PID"

echo "\n8. 等待服务启动..."
sleep 8

echo "\n9. 验证服务状态..."
echo "Node.js进程:"
ps aux | grep node | grep -v grep

echo "\n端口监听:"
netstat -tlnp | grep 8080

echo "\n服务日志:"
if [ -f "/var/log/backend.log" ]; then
    echo "=== 最新日志 ==="
    tail -20 /var/log/backend.log
fi

echo "\n10. 测试连接..."
echo "测试健康检查:"
curl -s -m 5 http://localhost:8080/health || echo "健康检查失败"

echo "测试API接口:"
curl -s -m 5 http://localhost:8080/api || echo "API测试失败"

echo "\n======================================"
echo "修复完成！"
echo "后端目录: $BACKEND_DIR"
echo "启动文件: $START_FILE"
echo "外部访问: http://47.237.10.129:8080"
echo "健康检查: http://47.237.10.129:8080/health"
echo "======================================"

echo "\n如果外部仍无法访问，请检查:"
echo "1. 阿里云安全组是否开放8080端口"
echo "2. 服务器防火墙设置"
echo "3. 查看完整日志: tail -f /var/log/backend.log"